/**
 * Object validator
 */
ninejs.extend(ninejs, function( exports ) {
	var validator = {
		types: {},
		messages: [],
		config: {},    
		validate: function (data) {
			var i, j, msg, typeList, type, checker, result_ok;
			
			this.messages = [];
			for ( i in data ) {
				if ( data.hasOwnProperty(i) ) {
					typeList = this.config[i] ? this.config[i].split(" ") : [];
					
					for ( j = 0; j < typeList.length; j++ ) {
						type = exports.trim(typeList[j]);
						checker = this.types[type];
						
						if ( !type ) {
							continue;
						}
						if ( !checker ) {
							throw "The validate function " + type + " is not exist!";
						}
				
						result_ok = checker.validate(data[i]);
						if ( !result_ok ) {
							msg = i + " " + checker.instructions;
							this.messages.push(msg);
						}
					}
				}
			}
			return this.hasErrors();
		},
		hasErrors: function () {
			return this.messages.length !== 0;
		}
	};

	// 添加验证项
	validator.types.isNonEmpty = {
		validate: function (value) {
			return !!value;
		},
		instructions: "不能为空！"
	};
	validator.types.isNumber = {
		validate: function (value) {
			return !isNaN(Number(value));
		},
		instructions: "只能是数值！"
	};
	validator.types.isAlphaNum = {
		validate: function (value) {
			return !String(value).replace(/[a-z0-9]/ig, "").length;
		},
		instructions: "不能包含特殊字符！"
	};

	exports.Validator = validator;
});